package com.zr.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.zr.dao.HangyeDao;
import com.zr.model.Content;
import com.zr.util.GetJDBCUtil;

public class HangyeDaoImpl implements HangyeDao{

	GetJDBCUtil gt = new GetJDBCUtil();
	
	@Override
	public int getTotalSourse() {
		Connection con = gt.getJDBCConnection();
		int total = 0;
		
		StringBuffer sql = new StringBuffer(""); 
		sql.append("SELECT count(e_content.cid) AS total ");
		sql.append("FROM e_content ");
		
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			
			ResultSet rs = pst.executeQuery();
			
			if(rs.next()){
				total = rs.getInt("total");
			}
			
			rs.close();
			pst.close();
			con.close();
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return total;
	}

	@Override
	public Content getContentSourse(int total) {
		Connection con = gt.getJDBCConnection();
		Content content = new Content();
		
		StringBuffer sql = new StringBuffer(""); 
		sql.append("SELECT * ");
		sql.append("FROM e_content ");
		sql.append("LIMIT ?,1 ");
		
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, total);
			ResultSet rs = pst.executeQuery();
			
			while(rs.next()){
				content.setCtitle(rs.getString("ctitle"));
			}
			
			rs.close();
			pst.close();
			con.close();
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return content;
	}

}
